package com.itheima.dao;

import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CheckGroupDao {

    /**
     * 模糊查询
     * @param queryString
     * @return
     */
    @Select("<script> " +
            "select * from t_checkgroup <where>\n" +
            "            <if test=\"value!=null and value.length > 0\">\n" +
            "                code like concat('%',#{value},'%') or name like concat('%',#{value},'%') or helpCode like concat('%',#{value},'%')\n" +
            "            </if>\n" +
            "        </where>" +
            "</script> ")
    List<CheckGroup> findByCondition(String queryString);

    /**
     * 通过id查询检查组
     * @param id
     * @return
     */
    @Select("select * from t_checkgroup where id =#{id}")
    CheckGroup findById(Integer id);

    /**
     * 查询所有
     * @param checkGroupId
     * @return
     */
    @Select("select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{checkGroupId}")
    List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId);

    /**
     * 添加检查组
     * @param checkGroup
     * @return
     */
    @Insert("insert into t_checkgroup values(null,#{code},#{name},#{helpCode},#{sex},#{remark},#{attention})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
        //拿到自动生成的主键
    Integer saveCheckGroup(CheckGroup checkGroup);

    /**
     * 添加检查组和检查项关系
     * @param id
     * @param checkItemId
     * @return
     */
    @Insert("insert into t_checkgroup_checkitem values(#{id},#{checkItemId})")
    Integer setCheckGroupAndCheckItem(@Param("id") Integer id, @Param("checkItemId") Integer checkItemId);

    /**
     * 删除中间表的关系
     * @param checkGroupId
     * @return
     */
    @Delete("delete from t_checkgroup_checkitem where checkgroup_id = #{checkGroupId}")
    Integer deleteAssocication(Integer checkGroupId); //删除中间表中的关系


    /**
     * 删除检查组
     * @param checkGroupId
     * @return
     */
    @Delete("delete from t_checkgroup where id=#{checkGroupId}")
    Integer deleteCheckGroup(Integer checkGroupId);


    /**
     * 更新检查组
     * @param checkGroup
     * @return
     */
    @Update("update t_checkgroup set code=#{code},name=#{name},helpCode=#{helpCode},sex=#{sex},remark=#{remark},attention=#{attention} where id = #{id}")
    Integer update(CheckGroup checkGroup);


    /**
     * 查询所有
     * @return
     */
    @Select("select * from t_checkgroup")
    List<CheckGroup> findAll();

    @Select("select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{setmealId}")
    List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId);

    @Select("select * from t_checkgroup tc,t_setmeal_checkgroup tsc where tc.id=tsc.checkgroup_id and tsc.setmeal_id=#{setmealId}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "id",property = "checkItems",
                    many = @Many(
                            select = "com.itheima.dao.CheckItemDao.findCheckItemByCheckGroupId"
                    )
            )
    })
    List<CheckGroup> findCheckGroupBySetmealId(Integer setmealId);

}
